/**
 * Created by wenchuan on 2018/5/10.
 */
define(function () {

    var self,
        app = win.app,
        others = {
            ischecked: '0',
            countAmount: 0
        },
        subOrders = [],
        pageParameters
    ;

    function setCountAmount() {
        var c = 0;
        subOrders.forEach(function (v) {
            if (v.ischecked == '1') {
                c += (Number(v.GWBK003) * Number(v.GWBK004));
            }
        });
        others.countAmount = c;
    }

    var QzsjGwc = app.Module.extend({

        init: function () {
            self = this;

            self._super({
                _readyBefore: function () {
                    pageParameters = self._pageParameters;
                    var items = pageParameters.items;
                    items.forEach(function (p1, p2, p3) {
                        var _p1 = app.cloneJSON(p1);
                        delete _p1.details;
                        _p1.ischecked = '1';
                        p1.details.forEach(function (p10, p20, p30) {
                            subOrders.push(app.extend(p10, _p1));
                        });
                    });
                }
            }, win);
        },
        subData: function (_subOrders) {
            app.showLoading();
            var len = _subOrders.length;

            var toData = function () {
                app.ajax({
                    xurl: "api/wx/gwat300/qzyxzdSubmit",
                    loading: true,
                    xdata: {
                        GWBKID: pageParameters.items[0].GWBKID,
                        GWBKNAME: pageParameters.items[0].GWBKNAME,
                        GWAKPLANT: pageParameters.items[0].GWAKPLANT,
                        GWAK056: pageParameters.items[0].GWAK056||'',
                        GWAK009: pageParameters.items[0].GWAK009,
                        objs: app.jsonString(_subOrders)
                    },
                    cb: function (result) {
                        if (result.ok) {
                            app.alert({
                                msg: '转单提交成功！',
                                cb: function () {
                                    app.back(1);
                                }
                            });
                            pageParameters.cb();
                        }
                    }
                });
            };

            var toSubmit = function (i) {
                var item = _subOrders[i];
                if (item.imgs && item.imgs.length > 0) {
                    app.uploadLocalIds({
                        imgs: item.imgs,
                        cb: function (imgs) {
                            delete item.imgs;
                            var imgurl = "";
                            for (var i = 0; i < imgs.length; i++) {
                                imgurl += (imgs[i].name + ",");
                            }
                            if (imgs.length > 0) {
                                if (item.GWCQ061) {
                                    item.GWCQ061 += ',' + imgurl.substring(0,imgurl.length-1);//图片地址
                                } else {
                                    item.GWCQ061 = imgurl.substring(0,imgurl.length-1);//图片地址
                                }
                            }

                            if (i == len - 1) {
                                toData();
                            } else {
                                toSubmit(i + 1);
                            }
                        }
                    });
                } else {
                    if (i == len - 1) {
                        toData();
                    } else {
                        toSubmit(i + 1);
                    }
                }
            };

            toSubmit(0);

        },
        _lazy: function () {
            this._super();
            new Vue({
                el: app.getByid(doc, 'scroll1'),
                data: {
                    items: subOrders
                },
                watch: {
                    items: {
                        handler: function (val, oldVal) {
                            setCountAmount();
                        },
                        deep: true
                    }
                },
                methods: {}
            });
            new Vue({
                el: app.getByid(doc, 'contentFooter'),
                data: others,
                methods: {
                    subOrder: function () {
                        var _subOrders = subOrders.filterKV({
                            ischecked: '1'
                        });
                        if (_subOrders.length > 0) {
                            app.confirm({
                                msg: '确定是否提交？',
                                ok: function (e) {
                                    self.subData(_subOrders);
                                }
                            });
                        }
                    }
                }
            });

            setCountAmount();

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        },
        _ready: function () {
            Vue.component('qzsjgwc-listitem', {
                template: app.getByid(doc, 'cartitem-template'),
                props: {
                    model: Array
                },
                methods: {
                    minus: function (val) {
                        if (val.GWBK003 > 1) {
                            val.GWBK003--;
                        }
                    },
                    plus: function (val) {
                        val.GWBK003++;
                    },
                    showDetail: function (val) {
                        val.GWAP100_1 = val.GWBK004;
                        app.openPage({
                            src: "gwcxd/qzsj-classes-info" + (val.status == 'Z' ? '-msg' : '') + ".html",
                            argsKey: 'classesinfo_page',
                            args: {
                                order: val,
                                cb: function () {

                                }
                            }
                        });
                    },
                    checkChange: function () {
                        setCountAmount();
                    }
                }
            });
        }
    });

    return new QzsjGwc();

});